﻿function PeopleHeadcanvas(id) {
    this.peopleHead_show_state = false;
    //
    var peopleHead_context = null;
    var peopleHead_size = { w: 80, h: 83 };
    var prevImg = null;
    var animate_fun = null;
    var arg = null;
    var animate_fun_name = "PeopleHeadfun";
    var draw_end_callback = null;
    var peopleHead_canvas = document.getElementById(id);
    var _this = this;
    //
    //----------------------------------------------
    //          Constructors
    //----------------------------------------------
    peopleHead_context = peopleHead_canvas.getContext("2d");
    //----------------------------------------------
    //          public
    //----------------------------------------------
    this.SetCallBack = function (callback) {
        draw_end_callback = callback;
    }
    this.SetArg = function (parameters) {
        if (parameters) {
            arg = arg || new Object();
            for (var i in parameters) {
                arg[i] = parameters[i];
            }
        }
    }
    this.NewAnimateFun = function (funname) {
        animate_fun_name = funname || animate_fun_name;
        try {
            eval("animate_fun=new " + animate_fun_name + "(peopleHead_context,callback,peopleHead_size)");
        }
        catch (ex) {
            alert("调用自定义PeopleHead动画函数出错:" + animate_fun_name);
        }
    }
    this.DrawPeopleHead = function (img) {
        this.peopleHead_show_state = true;
        if (!prevImg) {
            prevImg = img;
            animate_fun.Show(img, arg);
        }
        else {
            arg.prevImg = prevImg;
            prevImg = null;
            animate_fun.Hide(img, arg);
        }
    }
    this.ClearPeopleHead = function () {
        if (prevImg) {
            this.peopleHead_show_state = true;
            arg.prevImg = prevImg;
            animate_fun.Hide(null, arg);
        }
    }
    //----------------------------------------------
    //          private
    //----------------------------------------------
    function callback(img, fun, arg) {
        if (prevImg) {
            prevImg = img;
            _this.peopleHead_show_state = false;
            draw_end_callback();
            draw_end_callback = null;
        }
        else {
            prevImg = img;
            fun.Show(img, arg);
        }
    }
}
function PeopleHeadfun(peopleHead_context, callback, peopleHead_size) {
    var _peopleHead_context = peopleHead_context;
    var _callback = callback;
    var _peopleHead_size = peopleHead_size;
    var _this = this;
    this.Show = function (img, arg) {
        var x = peopleHead_size.w / 2, y = peopleHead_size.h / 2;
        Tool.Animate.Box_Show_Animate(img, _peopleHead_context.canvas, function () {
            _callback(img, _this, arg);
        }, x, y, img.width, img.height, config.boxAnimateTimeSpeed, 4);
    }
    this.Hide = function (img, arg) {
        Tool.Animate.Box_Hide_Animate(arg.prevImg, _peopleHead_context.canvas, function () {
            _callback(img, _this, arg);
        }, 0, 0, arg.prevImg.width, arg.prevImg.height, config.boxAnimateTimeSpeed, 4);
    }
}
//function PeopleHeadfun(peopleHead_context, callback, peopleHead_size) {
//    var _peopleHead_box_border_color = "#104E8B";
//    var _peopleHead_box_lineW = 3;
//    //
//    var _peopleHead_context = peopleHead_context;
//    var _callback = callback;
//    var _peopleHead_size = peopleHead_size;
//    this.Show = function (img, arg) {
//        _peopleHead_context.save();
//        _peopleHead_context.strokeStyle = _peopleHead_box_border_color;
//        _peopleHead_context.lineWidth = _peopleHead_box_lineW;
//        _peopleHead_context.clearRect(0, 0, _peopleHead_size.w + _peopleHead_box_lineW, _peopleHead_size.h + _peopleHead_box_lineW);
//        _peopleHead_context.strokeRect(_peopleHead_box_lineW / 2, _peopleHead_box_lineW / 2, _peopleHead_size.w + _peopleHead_box_lineW, _peopleHead_size.h + _peopleHead_box_lineW);
//        _peopleHead_context.drawImage(img, _peopleHead_box_lineW, _peopleHead_box_lineW);
//        _peopleHead_context.restore();
//        _callback(img, this, arg);
//    }
//    this.Hide = function (img, arg) {
//        _peopleHead_context.save();
//        _peopleHead_context.clearRect(0, 0, _peopleHead_size.w + _peopleHead_box_lineW * 2, _peopleHead_size.h + _peopleHead_box_lineW * 2);
//        _peopleHead_context.restore();
//        _callback(img, this, arg);
//    }
//}